package idv.loveloser.mobilemoodle;


import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;



import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class CourseEditor extends Activity {
	private SharedPreferences sharedPreferences ;
	private SharedPreferences.Editor edit=null;
	private String sessionKey; 
	private long client;
	private Spinner categoriesSpinner;
	private Button createCourseBtn;
	private Long categoryId=-1L;
	private EditText courseFullName,courseShortName;
	private Util util;
	private List<MdlCourseCategories> categories;
	@Override
	public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	   setContentView(R.layout.course_editor);
	   
	   categoriesSpinner = (Spinner) findViewById(R.id.categories_spinner);
	   courseFullName = (EditText) findViewById(R.id.course_full_name);
	   courseShortName = (EditText) findViewById(R.id.course_short_name);
	   sharedPreferences=getApplicationContext().getSharedPreferences("MySettings", MODE_WORLD_WRITEABLE );
	   client=sharedPreferences.getLong("client", -1L);
	   sessionKey= sharedPreferences.getString("sessionKey", "");
	   String responseStr=Util.getCategories(client, sessionKey);
	   Gson gson = new Gson();
	   categories = gson.fromJson(responseStr, new TypeToken<List<MdlCourseCategories>>(){}.getType()); 
	   String[] categoriesArray = new String[categories.size()];
	   for(int index=0;index<categories.size();index++){
		   
		   categoriesArray[index]=categories.get(index).getName();
	   }
	   ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,categoriesArray); 
	   categoriesSpinner.setAdapter(adapter);
	   categoriesSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
				long id) {
			MdlCourseCategories category = categories.get(position);
			categoryId =category.getId();		
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}
		   
	   });
	   createCourseBtn =(Button) findViewById(R.id.create_course_btn);
	   
	   createCourseBtn.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View v) {
			StrictMode.enableDefaults();
			String resultStr=Util.addCourse(client, sessionKey, courseShortName.getText().toString(), courseFullName.getText().toString(), categoryId);
			if(resultStr.length()==0)
				Toast.makeText(getApplicationContext(), "Create Fail!", Toast.LENGTH_SHORT).show();
			else
				finish();
		}
	});
	}
}
